package com.achievement.service.impl.authorImpl;

import com.achievement.domain.dto.author.ArtProductAuthorDTO;
import com.achievement.domain.entity.author.ArtProductAuthorEntity;
import com.achievement.domain.vo.author.ArtProductAuthorVO;
import com.achievement.mapper.author.ArtProductAuthorMapper;
import com.achievement.mappings.author.ArtProductAuthorMapping;
import com.achievement.service.author.IArtProductAuthorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 科研成果-艺术作品列-作者Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class ArtProductAuthorServiceImpl extends ServiceImpl<ArtProductAuthorMapper,ArtProductAuthorEntity> implements IArtProductAuthorService 
{
    @Autowired
    private ArtProductAuthorMapping mapping;


    @Override
    public List<ArtProductAuthorEntity> getListByConditions(ArtProductAuthorDTO authorDTO) {
        QueryWrapper<ArtProductAuthorEntity> queryWrapper = new QueryWrapper();
        List<ArtProductAuthorEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public ArtProductAuthorVO getInfoById(String id) {
        ArtProductAuthorEntity entity = this.getById(id);
        return mapping.entity2Vo(entity);
    }
    @Override
    public boolean add(ArtProductAuthorDTO dto) {
        ArtProductAuthorEntity entity = mapping.dto2Entity(dto);
        return this.updateById(entity);
    }

    @Override
    public boolean addBatch(List<ArtProductAuthorDTO> list,String artProductId) {
        this.batchRemoveByArtProductIds(Arrays.asList(artProductId));
        list.forEach(a->{
            a.setArtProductId(artProductId);
        });
        List<ArtProductAuthorEntity> addList = list.stream().map(mapping::dto2Entity)
                .collect(Collectors.toList());
        return this.saveBatch(addList, addList.size());
    }

    @Override
    public boolean update(ArtProductAuthorDTO dto) {
        ArtProductAuthorEntity entity = mapping.dto2Entity(dto);
        return this.updateById(entity);
    }

    @Override
    public boolean batchRemove(List<String> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public List<ArtProductAuthorVO> getListByArtProductId(String artProductId) {
        LambdaQueryWrapper<ArtProductAuthorEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ArtProductAuthorEntity::getArtProductId, artProductId);
        queryWrapper.orderByAsc(ArtProductAuthorEntity::getOrderId);
        List<ArtProductAuthorEntity> list= this.list(queryWrapper);
        return list.stream().map(mapping::entity2Vo).collect(Collectors.toList());

    }

    @Override
    public boolean batchRemoveByArtProductIds(List<String> artProductIds) {
        LambdaQueryWrapper<ArtProductAuthorEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(ArtProductAuthorEntity::getArtProductId, artProductIds);
        return this.remove(queryWrapper);
    }}
